package com.jason.apps.dashboard;

import android.annotation.SuppressLint;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.SubMenu;
import com.apps.realtyportal.HomeActivity;
import com.apps.realtyportal.MyFragmentPagerAdapterManageAgent;
import com.apps.realtyportal.R;
import com.ipaulpro.afilechooser.utils.FileUtils;
import com.shanu.apps.details.ShowPropertyDetail;

@SuppressLint({ "NewApi", "NewApi", "ParserError" })
public class ManageAgent extends SherlockFragmentActivity implements
		EditAccount.EditPersonOnClickSelectedListener {

	public static String Path = null;
	public static String DATE = null;

	public static Context appContext;
	ActionBar actionbar;

	private static final int DASHBOARD = 3;
	private static final int REQUEST_CODE = 1;
	private static final int REQUEST_EDIT = 2;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.displayhere);
		appContext = getApplicationContext();

		// ActionBar
		actionbar = getSupportActionBar();
		actionbar.setDisplayHomeAsUpEnabled(true);
		actionbar.setDisplayShowTitleEnabled(false);
		actionbar.setLogo(R.drawable.logo3);
		actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		ActionBar.Tab add = actionbar.newTab().setText("Add")
				.setIcon(R.drawable.add);
		ActionBar.Tab edit = actionbar.newTab().setText("Edit")
				.setIcon(R.drawable.edit);
		ActionBar.Tab delete = actionbar.newTab().setText("Delete")
				.setIcon(R.drawable.delete);

		Fragment AddFragment = new CreateAccount();
		Fragment EditFragment = new EditAccount();
		Fragment DeleteFragment = new DeleteAccount();

		add.setTabListener(new MyTabsListener(AddFragment));
		edit.setTabListener(new MyTabsListener(EditFragment));
		delete.setTabListener(new MyTabsListener(DeleteFragment));

		actionbar.addTab(add);
		actionbar.addTab(edit);
		actionbar.addTab(delete);

	}

	@Override
	public void onBackPressed() {
		Toast.makeText(getApplicationContext(), "Please use the up button",
				Toast.LENGTH_LONG).show();
	}

	@Override
	public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
		// menu.add(0, DASHBOARD, 0, "Search").setIcon(R.drawable.logouticon);
		//
		// return super.onCreateOptionsMenu(menu);
		SubMenu subMenu = menu.addSubMenu("Options");

		// subMenu.add("Logout").setIcon(R.drawable.logouticon);
		// subMenu.add("Go to Profile").setIcon(R.drawable.profile1);

		subMenu.add(0, 10, Menu.NONE, "Logout").setIcon(R.drawable.logouticon);
		subMenu.add(0, 15, Menu.NONE, "Go to Profile").setIcon(
				R.drawable.profile);

		MenuItem subMenu1Item = subMenu.getItem();
		subMenu1Item.setIcon(R.drawable.ic_action_overflow);
		subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			Intent intent = new Intent(this, HomeActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
			startActivity(intent);
		case 10:
			finish();
			startActivity(new Intent(getApplicationContext(),
					HomeActivity.class));
		case 15:
			finish();
		}
		return super.onOptionsItemSelected(item);
	}

	public void Browse(View v) {
		Intent target = FileUtils.createGetContentIntent();
		Intent intent = Intent.createChooser(target,
				getString(R.string.chooser_title));

		try {
			startActivityForResult(intent, REQUEST_CODE);
		} catch (ActivityNotFoundException e) {
		}
	}

	public void EditBrowse(View v) {
		Intent target = FileUtils.createGetContentIntent();
		Intent intent = Intent.createChooser(target,
				getString(R.string.chooser_title));

		try {
			startActivityForResult(intent, REQUEST_EDIT);
		} catch (ActivityNotFoundException e) {
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case REQUEST_CODE:
			if (resultCode == RESULT_OK) {
				if (data != null) {
					final Uri uri = data.getData();

					try {
						Path = getRealPathFromURI(uri);

						CreateAccount details = (CreateAccount) getSupportFragmentManager()
								.findFragmentById(R.id.fragment_container);
						details.setUrl(Path);

					} catch (Exception e) {
						Log.e("FileSelectorTestActivity",
								"File select error CREATEACCOUNT", e);
					}
				}

			}
			break;
		case REQUEST_EDIT:
			if (resultCode == RESULT_OK) {
				if (data != null) {
					final Uri uri = data.getData();

					try {
						Path = getRealPathFromURI(uri);

						PersonDetail person = (PersonDetail) getSupportFragmentManager()
								.findFragmentById(R.id.fragment_container);
						person.setUrl(Path);

					} catch (Exception e) {
						Log.e("FileSelectorTestActivity",
								"File select error PERSONDETAIL", e);
					}
				}

			}
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	public String getRealPathFromURI(Uri contentUri) {
		String[] proj = { MediaStore.Images.Media.DATA };
		Cursor cursor = getContentResolver().query(contentUri, proj, null,
				null, null);
		int column_index = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}

	/*
	 * public void onArticleSelected(View v, int position) { // TODO
	 * Auto-generated method stub PersonDetail newFragment = new PersonDetail();
	 * Bundle args = new Bundle(); args.putInt(PersonDetail.ARG_POSITION,
	 * position); newFragment.setArguments(args); FragmentTransaction
	 * transaction = getSupportFragmentManager() .beginTransaction();
	 * transaction.replace(R.id.fragment_container, newFragment);
	 * transaction.addToBackStack(null);
	 * 
	 * transaction.commit(); }
	 */

	public void EditAccountonArticleSelected(View v, int position) {
		// TODO Auto-generated method stub
		PersonDetail newFragment = new PersonDetail();
		Bundle args = new Bundle();
		args.putInt(PersonDetail.ARG_POSITION, position);
		newFragment.setArguments(args);
		FragmentTransaction transaction = getSupportFragmentManager()
				.beginTransaction();
		transaction.replace(R.id.fragment_container, newFragment);
		transaction.addToBackStack(null);

		transaction.commit();
	}

	// @Override
	// public void onBackPressed() {
	// // TODO Auto-generated method stub
	// super.onBackPressed();
	// this.finish();
	// }
	//
	// }

	@SuppressLint("ParserError")
	class MyTabsListener implements ActionBar.TabListener {
		public Fragment fragment;

		public MyTabsListener(Fragment fragment) {
			this.fragment = fragment;
		}

		public void onTabReselected(Tab tab, FragmentTransaction ft) {
			FragmentManager fm = this.fragment.getFragmentManager();
			fm.popBackStack();
		}

		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			ft.replace(R.id.fragment_container, fragment);
		}

		public void onTabUnselected(Tab tab, FragmentTransaction ft) {
			// ft.detach(fragment);
		}

	}
}
